package cn.iocoder.yudao.module.book.controller.app;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.book.controller.admin.book.vo.BookPageReqVO;
import cn.iocoder.yudao.module.book.controller.admin.book.vo.BookRespVO;
import cn.iocoder.yudao.module.book.controller.admin.book.vo.BookSaveReqVO;
import cn.iocoder.yudao.module.book.dal.dataobject.book.BookDO;
import cn.iocoder.yudao.module.book.service.admin.book.BookService;
import cn.iocoder.yudao.module.book.service.admin.bookshelf.BookShelfService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@RestController
@RequestMapping("/book/book")
public class AppBookController {

    @Resource
    private BookService bookService;

    @Resource
    private BookShelfService bookShelfService;

    @GetMapping("/page")
    @Operation(summary = "获得书籍分页")
    public CommonResult<PageResult<BookRespVO>> getBookPage(@Valid BookPageReqVO pageReqVO) {
        PageResult<BookDO> pageResult = bookService.getBookPage(pageReqVO);
        PageResult<BookRespVO> voPageResult = BeanUtils.toBean(pageResult, BookRespVO.class);
        List<BookRespVO> voPageResultList = voPageResult.getList();
        if(CollUtil.isNotEmpty(voPageResultList)){
            voPageResultList.forEach(vo -> {
                vo.setBookShelfName(bookShelfService.getBookShelfNameById(vo.getBookShelfId()));
            });
        }
        return success(voPageResult);
    }

}